package com.fz.us.admin.company.dao.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.base.dao.impl.BaseDaoImpl;
import com.fz.us.admin.company.dao.UserDao;
import com.fz.us.base.bean.BaseEnum;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Dao实现类 - 用户
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-27 。
 * ============================================================================
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl<User, String> implements UserDao {

    @Override
    public User getUserByAdminId(String adminId) {
        Assert.hasText(adminId, "adminId is required");
        String hql = "from " + User.class.getName() + " model where model.adminId = :adminId ";
        return (User) getSession().createQuery(hql).setParameter("adminId", adminId).uniqueResult();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> getAllUsers(Company company) {
        Assert.notNull(company, "company is required");
        String hql = "from " + User.class.getName() + " model where model.company = :company ";
        return getSession().createQuery(hql).setParameter("company", company).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> getAllUsers(Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");
        Assert.notEmpty(states, "states is required");
        String hql = "from " + User.class.getName() + " model where model.company = :company and model.state in :states ";
        return getSession().createQuery(hql).setParameter("company", company).setParameterList("states", states).list();
    }

    @Override
    public User getUserByMobile(String mobile) {
        String hql = "from " + User.class.getName() + " model where model.mobile = ? and model.state = ?";
        List<User> usersList = getSession().createQuery(hql).setParameter(0, mobile).setParameter(1, BaseEnum.StateEnum.Enable).list();
        return usersList.size()>0 ? usersList.get(0) : null;
    }

}
